<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品销量控制
 *
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */

quanli_require('module/base/goods/quanliGoods.class.php');
class ajax_goodsSaleControl {

    public function execute(){
        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //检查商品是否存在 存在返回商品销量
                    $this->checkGoods();
                    break;

                case 2: //保存可变销量
                    $this->saveGoodsList();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }
    }

    /*
     *
     * 检查商品是否存在 存在返回商品销量
     */
    public function checkGoods(){
        global $G_PGLOB;

        if( !isset( $_REQUEST['cityID'],$_REQUEST['goodsName'] ) || empty( $_REQUEST['cityID'] ) || empty( $_REQUEST['goodsName'] ) ){
            returnJsonData('参数错误');
        }

        //实例化商品类
        $quanliGoods = new quanliGoods( $G_PGLOB->DBCA );

        $arg_array = array(
            'cityID' => $_REQUEST['cityID'],
            'extend' => ' AND goodsName = "' .$_REQUEST['goodsName'].'"'
        );

        $goods_list_array = $quanliGoods->searchList( $arg_array );

        if( !$goods_list_array ){
            returnJsonData('该商品不存在',404);
        }

        $return_array = array(
            'postID'=>$goods_list_array[0]['postID'],
            'saletotal'=>$goods_list_array[0]['saletotal']
        );

        returnJsonData('ok',200,$return_array);

    }

    /*
     *
     * 保存可变销量
     */
    public function saveGoodsList(){
        global $G_PGLOB;

        if (!isset($_REQUEST['goodsName'], $_REQUEST['postID'], $_REQUEST['cityID'],$_REQUEST['saletotal'],$_REQUEST['changeSaleTotal'],$_REQUEST['changeSaleType'])
            || empty($_REQUEST['goodsName']) || empty($_REQUEST['postID']) || !quanli_is_int($_REQUEST['saletotal'],2) || !quanli_is_int($_REQUEST['changeSaleTotal']) ) {
            returnJsonData( '参数错误',404 );
        }

        if(  $_REQUEST['changeSaleType'] == 1){

            $saletotal = intval($_REQUEST['saletotal']);
            $changeSaleTotal = intval( $_REQUEST['changeSaleTotal'] );
            if( $saletotal < $changeSaleTotal ){
                returnJsonData( '可变类型为减少时，可变数量不能大于商品销量',400 );
            }
        }
        $goods_list_obj = new entity( 'goods_list' );
        $goods_list_obj->postID = $_REQUEST['postID'];

        if( !$G_PGLOB->DBCA->fetch( $goods_list_obj ) ){
            returnJsonData( '该商品不存在',404 );
        }

        $goods_list_obj->changeSaleType = $_REQUEST['changeSaleType'];
        $goods_list_obj->changeSaleTotal = $_REQUEST['changeSaleTotal'];

        $G_PGLOB->DBCA->save( $goods_list_obj );

        returnJsonData( '可变销量保存成功',200 );
    }
};